package com.ruoyi.novel.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.novel.domain.Invitation;
import com.ruoyi.novel.service.IInvitationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 邀请链接Controller
 *
 * @author å¼ ä¸
 * @date 2025-06-12
 */
@RestController
@RequestMapping("/novel/invitation")
public class InvitationController extends BaseController
{
    @Autowired
    private IInvitationService invitationService;

    /**
     * 查询邀请链接列表
     */
    @PreAuthorize("@ss.hasPermi('novel:invitation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Invitation invitation)
    {
        Long userId = SecurityUtils.getUserId();
        invitation.setUserId(Math.toIntExact(userId));
        startPage();
        List<Invitation> list = invitationService.selectInvitationList(invitation);
        return getDataTable(list);
    }

    /**
     * 导出邀请链接列表
     */
    @PreAuthorize("@ss.hasPermi('novel:invitation:export')")
    @Log(title = "邀请链接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Invitation invitation)
    {
        List<Invitation> list = invitationService.selectInvitationList(invitation);
        ExcelUtil<Invitation> util = new ExcelUtil<Invitation>(Invitation.class);
        util.exportExcel(response, list, "邀请链接数据");
    }

    /**
     * 获取邀请链接详细信息
     */
    @PreAuthorize("@ss.hasPermi('novel:invitation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(invitationService.selectInvitationById(id));
    }

    /**
     * 新增邀请链接
     */
    @PreAuthorize("@ss.hasPermi('novel:invitation:add')")
    @Log(title = "邀请链接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Invitation invitation)
    {
        Long userId = SecurityUtils.getUserId();
        invitation.setUserId(Math.toIntExact(userId));
        return toAjax(invitationService.insertInvitation(invitation));
    }

    /**
     * 修改邀请链接
     */
    @PreAuthorize("@ss.hasPermi('novel:invitation:edit')")
    @Log(title = "邀请链接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Invitation invitation)
    {
        Long userId = SecurityUtils.getUserId();
        invitation.setUserId(Math.toIntExact(userId));
        return toAjax(invitationService.updateInvitation(invitation));
    }

    /**
     * 删除邀请链接
     */
    @PreAuthorize("@ss.hasPermi('novel:invitation:remove')")
    @Log(title = "邀请链接", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(invitationService.deleteInvitationByIds(ids));
    }
}
